﻿using System;

namespace Quail
{
    /// <summary>
    /// Wraps access to an input tag of type file
    /// </summary>
    public class WebInputFile : WebInput
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WebInputFile"/> class.
        /// </summary>
        /// <param name="page">The owning container.</param>
        /// <param name="selector">The selector.</param>
        public WebInputFile(WebControlContainer page, string selector)
            : base(page, selector)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WebInputFile"/> class.
        /// </summary>
        /// <param name="control">The control.
        /// </param>
        public WebInputFile(WebControl control)
            : base(control)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WebInputFile"/> class.
        /// </summary>
        public WebInputFile()
        {
        }

        /// <summary>
        /// Gets or sets the input's value
        /// </summary>
        public override string Value
        {
            get
            {
                return base.Value;
            }

            set
            {
                if (((SeleniumTestSession)Browser.TestSession).BrowserType == BrowserTypes.FireFox)
                    base.Value = value;
                else
                {
                    var task = BeginTask("Set Value");
                    task.AddAttribute("Value", value);
                    try
                    {
                        var winName = Page.Title + " - Windows Internet Explorer";
                        var keys = value;

                        Page.Browser.SeleniumInterface.WindowFocus();
                        Page.Browser.SeleniumInterface.Focus(Locator);
                        System.Windows.Forms.SendKeys.SendWait(value);
                        task.Finish();
                    }
                    catch (Exception e)
                    {
                        task.Finish(e);
                        throw;
                    }

                    // Browser.Processor.DoCommand("sendKeys", new string[] { winName, keys });
                    // Page.Browser.Selenium.TypeKeys(Locator,value);
                }
            }
        }
    }
}
